package com.zhf.cloud.gateway.springcloudgateway;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

/**
 * @author: 曾鸿发
 * @create: 2021-11-18 17:38
 * @description：gateway项目启动类
 **/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudGatewayApplication.class, args);
    }

    @Value("${Aj_system_center}")
    private String Aj_system_center;

    @Bean
    public RequestInterceptor cloudContextInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                String url = template.url();
                if(url.contains("$Aj_system_center")){
                    url = url.replace("$Aj_system_center", Aj_system_center);
                    template.uri(url);
                }

                if(url.startsWith("//")){
                    url = "http:" + url;
                    template.target(url);
                    template.uri("");
                }
            }
        };
    }
}
